package com.onluy.frameworkcloud.tools.utils;


import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.util.Vector;

public class FontImageUtil {


    public static void createStringImage(String str,   File outFile) throws Exception {

        createStringImage( str ,-1,5, new Font("楷体", Font.BOLD, 30),    Color.black,false,outFile);
    }
    // 根据str,font的样式以及输出文件目录
    public static void createStringImage(String str,int maxWidth,int yspace, Font font,Color fontColor ,Boolean iscenter , File outFile) throws Exception {
        if(outFile.exists())throw  new Exception("文件已经存在！");

        FontMetrics fm = sun.font.FontDesignMetrics.getMetrics(font);
        int strmaxwidth=100000;
        if(maxWidth>0){
            strmaxwidth=maxWidth;
        }
        String[] formatstr=format(str, strmaxwidth, font);
        int width =10;
        int height=0;
        int space=5;
        int x,y;
        int tempwidth=10;
        int fontheight=fm.getHeight();

        for (int i = 0; i < formatstr.length; i++) {
            tempwidth=fm.stringWidth(formatstr[i]);
            if(tempwidth>width)width=tempwidth+space*2;
            height+=fontheight+yspace;

        }
        //height+= yspace*2;

        if(maxWidth>0){
            width=maxWidth+space*2;
        }

        // 创建图片
        BufferedImage image = new BufferedImage(width, height,  BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = image.createGraphics();
        g.setClip(0, 0, width, height);
        g.setColor(fontColor);// 颜色字体
        g.setFont(font);// 设置画笔字体
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB); //消除锯齿状

        /** 用于获得垂直居中y */
        for (int i = 0; i < formatstr.length; i++) {// 256 340 0 680
            if(iscenter){
                tempwidth=fm.stringWidth(formatstr[i]);
                x=(width-tempwidth)/2;
                x=x<=0?space:x;
            }else{
                x=space;
            }
            y=i*yspace+(i+1)*fontheight;
            g.drawString(formatstr[i], x, y);// 画出字符串
        }
        g.dispose();

        ImageIO.write(image, "png", outFile);// 输出png图片
    }



    // 字符串切割,实现字符串自动换行
    public static String[] format(String text, int maxWidth, Font ft) {
        FontMetrics fm = sun.font.FontDesignMetrics.getMetrics(ft);
        String[] result = null;
        Vector tempR = new Vector();
        int lines = 0;
        int len = text.length();
        int index0 = 0;
        int index1 = 0;
        boolean wrap;
        while (true) {
            int widthes = 0;
            wrap = false;
            for (index0 = index1; index1 < len; index1++) {
                if (text.charAt(index1) == '\n') {
                    index1++;
                    wrap = true;
                    break;
                }
                widthes = fm.charWidth(text.charAt(index1)) + widthes;
                if (widthes > maxWidth) {
                    break;
                }
            }
            lines++;

            if (wrap) {
                tempR.addElement(text.substring(index0, index1 - 1));
            } else {
                tempR.addElement(text.substring(index0, index1));
            }
            if (index1 >= len) {
                break;
            }
        }
        result = new String[lines];
        tempR.copyInto(result);
        return result;
    }

    public static Color fromStrToARGB(String str) {
        Color color=Color.BLACK;
        try {
            String str2 = str.substring(0, 2);
            String str3 = str.substring(2, 4);
            String str4 = str.substring(4, 6);
            int red = Integer.parseInt(str2, 16);
            int green = Integer.parseInt(str3, 16);
            int blue = Integer.parseInt(str4, 16);
            color = new Color(red, green, blue);
        }catch (Exception ex){
            //ex.printStackTrace();
            color=Color.red;
        }
        return color;
    }

    public  static Font loadFontByName(String tdclogoFilePath,int style, int size  )  //第一个参数是外部字体名，第二个是字体大小
    {
        try
        {
            File file = new File(tdclogoFilePath);
            FileInputStream aixing = new FileInputStream(file);
            Font dynamicFont = Font.createFont(Font.TRUETYPE_FONT, aixing);
            Font dynamicFontPt = dynamicFont.deriveFont(style,size);

            aixing.close();
            return dynamicFontPt;
        }
        catch(Exception e)//异常处理
        {
            //e.printStackTrace();
            return new java.awt.Font("宋体", style, size);
        }
    }
}
